package com.gemframework.util;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * See {@link org.springframework.util.SocketUtils}
 *
 */
public class PortUtil {

    /**
     * the port is available or not
     * @param port
     * @return
     */
    public static boolean isAvailable(int port) {
        return isAvailable(null, port);
    }

    /**
     * the port is available or not
     * @param host
     * @param port
     * @return
     */
    public static boolean isAvailable(String host, int port) {
        try (Socket ignored = new Socket(host, port)) {
            return false;
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            return true;
        }
    }
}
